﻿// See https://aka.ms/new-console-template for more information
using FreeSql.DataAnnotations;
using System.Diagnostics;
using System.Text.Json.Nodes;
using System.Text.Json;


Console.WriteLine("Hello, World!");

// 配置序列化选项以实现缩进
var jsonOpt = new JsonSerializerOptions
{
    WriteIndented = true // 设置缩进
};

var fsql = new FreeSql.FreeSqlBuilder()
        //.UseMonitorCommand(cmd => Trace.WriteLine($"Sql：{cmd.CommandText}"))
        .UseAdoConnectionPool(true)
        .UseConnectionString(FreeSql.DataType.MySql, @"Data Source=ali.**.cn;Port=3306;User ID=root;Password=27**981;Initial Catalog=bark;")
        .UseAutoSyncStructure(false) //自动同步实体结构到数据库，只有CRUD时才会生成表
        .Build();

fsql.Aop.CommandBefore += (s, e) =>
{
    Console.WriteLine($"CommandBefore:\r\n{e.Command.CommandText}\r\n");
};

fsql.Aop.CurdAfter += (s, e) =>
{ 
    Console.WriteLine($"{e.ElapsedMilliseconds}ms;\n{e.Sql}\r\n");
};

var locs =  fsql.Select<wallet_rs_location>()
    .Where(a => a.status != "DELETE")
    .ToList();
var locs_str = JsonSerializer.Serialize(locs, jsonOpt);

Console.WriteLine(locs_str);

var dataPermission = fsql.Select<DataPermission>()
    .Where(a => a.status == 0 )
    .ToList();
var wallet_data_permission_str = JsonSerializer.Serialize(dataPermission);

Console.WriteLine(wallet_data_permission_str);

// 查询 DataPermission 中status = 0 并且 data_module = wallet_rs_placement 或者 wallet_rs_location的数据
var specificDataPermission = fsql.Select<DataPermission>()
    .Where(a => a.status == 0 && (a.DataModule == "wallet_rs_placement" || a.DataModule == "wallet_rs_location"))
    .ToList();
var specificDataPermissionStr = JsonSerializer.Serialize(specificDataPermission);

Console.WriteLine(specificDataPermissionStr);


// 筛选出 wallet_rs_placement的数据, 并且映射资源位置数据
var placementData = fsql.Select<DataPermission>()
    .Where(a => a.status == 0 && a.DataModule == "wallet_rs_placement")
    .ToList();

var placementMap = new Dictionary<string, placement_item>();
foreach (var item in placementData)
{
    if (!placementMap.ContainsKey(item.DataKey))
    {
        placementMap[item.DataKey] = new placement_item
        {
            location_tag_list = new string[] { /* 这里需要根据实际情况填充 location_tag 数据 */ },
            approve1 = new string[] { /* 这里需要根据实际情况填充 approve1 数据 */ },
            approve2 = new string[] { /* 这里需要根据实际情况填充 approve2 数据 */ }
        };
    }
    // 可以根据实际逻辑更新已存在的 placement_item 数据
}

// 输出映射结果
var placementMapStr = JsonSerializer.Serialize(placementMap, jsonOpt);
Console.WriteLine(placementMapStr);



Console.ReadLine();

